package cn.hebutrun.oulabot.common.util;

import cn.hebutrun.oulabot.common.Constants;
import cn.hebutrun.oulabot.common.CustomException;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONUtil;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.quartz.TriggerKey;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author zhaolong
 * @since 2022-03-10
 */
@Slf4j
@UtilityClass
public class LogUtil {
  public void error(String url, Exception e) {
    String sb = "\n【请求地址】:" + url + "\n【错误信息】:" + ExceptionUtils.getStackTrace(e);
    log.error(sb);
  }

  public void error(HttpServletRequest request, Exception e) {
    String sb =
        "\n【错误类型】:"
            + Exception.class
            + "\n【请求地址】:"
            + request.getRequestURL()
            + "\n【请求参数】:"
            + JSONUtil.toJsonStr(request.getParameterMap())
            + "\n【错误信息】:"
            + ExceptionUtils.getStackTrace(e);
    log.error(sb);
  }

  public void error(HttpServletRequest request, CustomException e) {
    String sb =
        "\n【错误类型】:"
            + CustomException.class
            + "\n【请求地址】:"
            + request.getRequestURL()
            + "\n【请求参数】:"
            + JSONUtil.toJsonStr(request.getParameterMap())
            + "\n【错误信息】:"
            + e.getMsg();
    log.error(sb);
  }

  public void error(HttpServletRequest request, String msg) {
    String sb =
        "\n【错误类型】:"
            + CustomException.class
            + "\n【请求地址】:"
            + request.getRequestURL()
            + "\n【请求参数】:"
            + JSONUtil.toJsonStr(request.getParameterMap())
            + "\n【错误信息】:"
            + msg;
    log.error(sb);
  }

  public void error(String msg) {
    String sb = "\n【错误信息】:" + msg;
    log.error(sb);
  }

  public void warn(HttpServletRequest request, String msg) {
    String sb =
        "\n【请求地址】:"
            + request.getRequestURL()
            + "\n【请求参数】:"
            + JSONUtil.toJsonStr(request.getParameterMap())
            + "\n【警告信息】:"
            + msg;
    log.warn(sb);
  }

  public static void warn(String msg, Exception e) {
    String sb = "\n【警告类型】:" + e.getClass() + "\n【警告信息】:" + msg;
    log.warn(sb);
  }

  public void warn(String msg) {
    String sb = "\n【警告信息】:" + msg;
    log.warn(sb);
  }

  public void info(String s) {
    String sb = "\n【日志信息】:" + s;
    log.info(sb);
  }

  /**
   * attribute日志追加
   *
   * @param httpRequest http请求
   * @param logInfo 日志信息
   * @date 2022/3/30
   * @author zhaolong
   */
  public void attributeLogAppend(HttpServletRequest httpRequest, String logInfo) {
    StringBuilder sb = (StringBuilder) httpRequest.getAttribute(Constants.LOG_ATTRIBUTE_NAME);
    if (sb == null) {
      return;
    }
    sb.append(logInfo);
    httpRequest.setAttribute(Constants.LOG_ATTRIBUTE_NAME, sb);
  }

  /**
   * 打印定时任务日志
   *
   * @param context 任务内容
   */
  public void quartzExecuteInfo(JobExecutionContext context) {
    JobKey key = context.getJobDetail().getKey();
    log.info(
        "\n【定时任务】: {} - {} 执行\n【任务描述】: {}\n【执行时间】: {}",
        key.getGroup(),
        key.getName(),
        context.getJobDetail().getDescription(),
        LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
  }

  public void quartzStartInfo(JobDetail jobDetail) {
    JobKey key = jobDetail.getKey();
    log.info(
        "\n【定时任务】: {} - {} 启动\n【任务描述】: {}\n【启动时间】: {}",
        key.getGroup(),
        key.getName(),
        jobDetail.getDescription(),
        LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
  }

  public void quartzStartError(TriggerKey triggerKey, String errorMessage) {
    log.error(
        "\n【定时任务】: {} - {} 启动失败\n【失败原因】: {}\n【启动时间】: {}",
        triggerKey.getGroup(),
        triggerKey.getName(),
        errorMessage,
        LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
  }

  public void quartzDeleteInfo(String info) {
    log.info(
        "\n【定时任务】: 删除\n【相关信息】: {}\n【删除时间】: {}",
        info,
        LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
  }

  public static void quartzDeleteError(JobKey jobKey, String info) {
    log.error(
        "\n【定时任务】: {} - {} 删除失败\n【相关信息】: {}\n【删除时间】: {}",
        jobKey.getGroup(),
        jobKey.getName(),
        info,
        LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
  }
}
